package com.breeze.toolkit.ui;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.breeze.toolkit.util.ConstantsUtil;
import com.breeze.toolkit.util.ResUtil;
import com.breeze.toolkit.util.ToastUtil;
import com.breeze.toolkit.util.Util;

/**
 * Created by john on 2016/8/31.
 */
public class RefreshView extends RelativeLayout implements View.OnClickListener {
	private Context mContext;
	private ImageView mImage;
	private TextView mNotice;
	private TextView mSolution;
	private OnRefreshListener mListener;
	private boolean mIsRefreshable = true;

	public RefreshView(Context context) {
		this(context, null);
	}

	public RefreshView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		mContext = getContext();
		setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		setClickable(true);
		setOnClickListener(this);
		setVisibility(GONE);
		View child = LayoutInflater.from(getContext()).inflate(
				ResUtil.getLayoutId(mContext, ConstantsUtil.REFRESH_VIEW_LAYOUT_NAME),
				this,
				false);
		if (child != null) {
			mImage = (ImageView) child.findViewById(ResUtil.getViewId(mContext, ConstantsUtil.TIP_IMAGE_ID_NAME));
			mNotice = (TextView) child.findViewById(ResUtil.getViewId(mContext, ConstantsUtil.TIP_NOTICE_ID_NAME));
			mSolution = (TextView) child.findViewById(ResUtil.getViewId(mContext, ConstantsUtil.TIP_SOLUTION_ID_NAME));
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			child.setLayoutParams(params);
			addView(child);
		}
	}

	/**
	 * 设置刷新页面展示内容
	 * @param resId 刷新页面图片资源ID
	 * @param notice 提示信息文案
	 * @param solution 解决办法文案
	 */
	public void setViewContent(int resId, String notice, String solution) {
		if (resId > 0) {
			mImage.setBackgroundResource(resId);
		}

		if (!TextUtils.isEmpty(notice)) {
			mNotice.setText(notice);
			mNotice.setVisibility(View.VISIBLE);
		} else {
			mNotice.setVisibility(View.GONE);
		}

		if (!TextUtils.isEmpty(solution)) {
			mSolution.setText(solution);
			mSolution.setVisibility(View.VISIBLE);
		} else {
			mSolution.setVisibility(View.GONE);
		}
	}

	/**
	 * 设置页面是否可重新加载
	 * @param refreshable true 可刷新（无网或连接异常） false 不可刷新（无数据）
	 */
	public void setRefreshable(boolean refreshable) {
		mIsRefreshable = refreshable;
	}

	/**
	 * 更新页面可见性
	 * @param show true 展示 false 隐藏
	 */
	public void updateVisibility(boolean show) {
		setVisibility(show ? View.VISIBLE : View.GONE);
	}

	@Override
	public void onClick(View v) {
		if (mListener != null && mIsRefreshable) {
			if (Util.isNetworkConnected(mContext)) {
				updateVisibility(false);
				mListener.refreshRequest();
			} else {
				ToastUtil.showNoNetworkToast(mContext);
			}
		}
	}

	public void setRefreshListener(OnRefreshListener listener) {
		mListener = listener;
	}

	@FunctionalInterface
	public interface OnRefreshListener {
		void refreshRequest();
	}
}
